public class Solution {
    public static int findLongest(String A, int n, String B, int m) {
        if(n==0 || m==0)return 0;
        int max = 0;
        char[] a = A.toCharArray();
        char[] b = B.toCharArray();
        int[][] dp = new int[n][m];
        for(int i=0; i<n; i++){
            for(int j=0; j<m;j++){
                if(a[i]==b[j]){
                    if(i>=1 && j>=1){
                        dp[i][j] = dp[i-1][j-1]+1;
                    }else{
                        dp[i][j]=1;
                    }
                }else{
                    dp[i][j]=0;
                }
                if(dp[i][j]>max)
                    max = dp[i][j];
            }

        }
        return max;
    }
    public static void main(String args[]){
        String a = "abc";
        String b = "abx";
        int c = findLongest(a,a.length(),b,b.length());
        System.out.println(c);
    }

}

